﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes

Public Class frmChietTinh

    Private Sub LoadTree()
        If Me.datTuNgay.EditValue Is Nothing Then Exit Sub
        If Me.datDenNgay.EditValue Is Nothing Then Exit Sub

        With Me.TreeList1
            .OptionsBehavior.Editable = False
            .OptionsView.ShowColumns = False
            .OptionsView.ShowHorzLines = False
            .OptionsView.ShowVertLines = False
            .OptionsView.ShowIndicator = False

            Me.TreeListColumn1.Visible = False
            Me.TreeListColumn2.Width = 250
            Me.TreeListColumn3.Visible = False

            Dim Node As TreeListNode = Nothing
            Dim Node0 As TreeListNode = Nothing
            Dim Node1 As TreeListNode = Nothing

            .Nodes.Clear()

            Dim myChietTinhF As New ChietTinhF
            Dim myDtb As New DataTable
            Dim i As Integer

            'all
            Node0 = Me.TreeList1.AppendNode(New Object() {0, "Chiết tính", ""}, Node)

            'loai chiet tinh
            myDtb = myChietTinhF.GetListLoaiChietTinh(Me.datTuNgay.EditValue, Me.datDenNgay.EditValue)
            For i = 0 To myDtb.Rows.Count - 1
                Node1 = .AppendNode(New Object() {1, myDtb.Rows(i).Item("maloaichiettinh") + "   " + myDtb.Rows(i).Item("tenloaichiettinh"), myDtb.Rows(i).Item("maloaichiettinh")}, Node0)
            Next

            .ExpandAll()
        End With
    End Sub

    Private Sub InitGrid()
        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "chon"
            .Columns(i).Caption = " "
            .Columns(i).Width = 20
            If Project_Form = "frmMenu" Then
                .Columns(i).Visible = False
            Else
                .Columns(i).Visible = True
            End If

            i = i + 1
            .Columns(i).FieldName = "sophieu"
            .Columns(i).Caption = "Số phiếu"
            .Columns(i).Width = 35

            i = i + 1
            .Columns(i).FieldName = "so_don"
            .Columns(i).Caption = "Số đơn"
            .Columns(i).Width = 50

            i = i + 1
            .Columns(i).FieldName = "ngaylap"
            .Columns(i).Caption = "Ngày lập"
            .Columns(i).Width = 40

            i = i + 1
            .Columns(i).FieldName = "khachhang"
            .Columns(i).Caption = "Khách hàng"
            .Columns(i).Width = 80

            i = i + 1
            .Columns(i).FieldName = "diachi"
            .Columns(i).Caption = "Địa chỉ"
            .Columns(i).Width = 140

            For i = 1 To .Columns.Count - 1
                .Columns(i).OptionsColumn.AllowEdit = False
            Next

        End With


    End Sub

    Private Sub LoadGrid()
        If Me.datTuNgay.EditValue Is Nothing Then Exit Sub
        If Me.datDenNgay.EditValue Is Nothing Then Exit Sub
        If Me.TreeList1.FocusedNode Is Nothing Then Exit Sub
        Dim myDtb As New DataTable

        Select Case Project_Form
            Case "frmMenu"
                Dim myChietTinhF As New ChietTinhF
                myDtb = myChietTinhF.GetWhere(Me.TreeList1.FocusedNode.Item(2), Me.datTuNgay.EditValue, Me.datDenNgay.EditValue)

            Case "frmThiCong_CapNhat"
                Dim myThiCongF As New ThiCongF
                myDtb = myThiCongF.GetChietTinh(Me.TreeList1.FocusedNode.Item(2), Me.datTuNgay.EditValue, Me.datDenNgay.EditValue)

                'xoa bot cac dong da co trong _dtb
                Dim r As Integer
                For r = 0 To myDtb.Rows.Count - 1
                    If ExitstValueInDTB(myDtb.Rows(r).Item("sophieu"), "sophieu", frmThiCong_CapNhat_Dtb) Then
                        myDtb.Rows(r).Delete()
                    End If
                Next
                frmThiCong_CapNhat_Dtb.Rows.Clear()

        End Select

       

      

        Me.GridControl1.DataSource = myDtb

    End Sub

    Private Sub frmChietTinh_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.datTuNgay.EditValue = CDate("1/" + (Now.Month).ToString + "/" + Now.Year.ToString)
        Me.datDenNgay.EditValue = Now.Date

        InitGrid()
        LoadTree()

        If Project_Form = "frmMenu" Then
            Me.GridControl1.Height = Me.btnChon.Top - Me.GridControl1.Top + Me.btnChon.Height
            Me.btnChon.Visible = False
        Else
            Me.GridControl1.Height = Me.btnChon.Top - Me.GridControl1.Top - 6
            Me.btnChon.Visible = True
        End If

    End Sub

    Private Sub TreeList1_FocusedNodeChanged(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.FocusedNodeChangedEventArgs) Handles TreeList1.FocusedNodeChanged
        LoadGrid()
    End Sub

    Private Sub GridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DoubleClick
        If Project_Form = "frmMenu" Then
            If frmChietTinh_CapNhat_Exists Then Exit Sub
            frmChietTinh_CapNhat_Exists = True

            frmChietTinh_CapNhat_SoPhieu = Me.GridView1.GetFocusedRowCellValue("sophieu")
            frmChietTinh_CapNhat_AddEdit = "edit"
            Dim f As New frmChietTinh_CapNhat
            f.Text = "Sửa chiết tính"
            f.MdiParent = _frmMenu
            f.Show()
        End If
    End Sub

    Private Sub datTuNgay_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles datTuNgay.EditValueChanged
        LoadTree()
        LoadGrid()
    End Sub

    Private Sub datDenNgay_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles datDenNgay.EditValueChanged
        LoadTree()
        LoadGrid()
    End Sub

    Private Sub Chon()
        frmChietTinh_Dtb = New System.Data.DataTable("MyDTB")
        frmChietTinh_Dtb.Columns.Add("sophieu", GetType(String))

        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            If Me.GridView1.GetRowCellValue(i, "chon") Then
                frmChietTinh_Dtb.Rows.Add(New Object() {Me.GridView1.GetRowCellValue(i, "sophieu")})
            End If
        Next

        Me.Close()
    End Sub

    Private Sub btnChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChon.Click
        Chon()
    End Sub

    Private Sub btnThemChietTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemChietTinh.Click
        If frmChietTinh_CapNhat_Exists Then Exit Sub
        frmChietTinh_CapNhat_Exists = True

        Project_Form = Me.Name
        frmChietTinh_CapNhat_AddEdit = "add"
        Dim f As New frmChietTinh_CapNhat
        f.Text = "Lập chiết tính"
        f.MdiParent = _frmMenu
        f.Show()
    End Sub

End Class